/*
 * Copyright (c) 2024 Huawei Technologies Co.,Ltd.
 *
 * openGauss is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 * http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * ---------------------------------------------------------------------------------------
 *
 * imcs_mem_alloc.h
 * routines to support ColStore
 *
 *
 * IDENTIFICATION
 * src/include/access/imcs/imcs_mem_alloc.h
 *
 * ---------------------------------------------------------------------------------------
 */
#ifndef IMCS_MEM_ALLOC_H
#define IMCS_MEM_ALLOC_H

#include "postgres.h"
#include "knl/knl_variable.h"

#define ImcsMaxPointersArryLen 512U
#define ImcsMaxPtrNodeCacheLen 256U

typedef struct IMPointerNode {
    void *ptr;
    IMPointerNode *next;

    void Reset()
    {
        ptr = NULL;
        next = NULL;
    }
} IMPointerNode;

typedef struct IMPointerList {
    IMPointerNode *header;
    IMPointerNode *tail;
} IMNodeList;
/*
 * CStoreMemAlloc
 * It manages the memory pointer from malloc
 * When transaction occurs erro, the memory from malloc can be reset in abortTransaction
 * When transaction commit, the memory from malloc can be reset
 */
class IMCSMemAlloc {
public:
    static void *Palloc(Size size, bool to_register = true);
    static void Pfree(void *pointer, bool registered = true);
    static void *Repalloc(void *pointer, Size size, Size old_size, bool registered = true);
    static void Register(void *pointer);
    static void Unregister(const void *pointer);
    static void Reset();
    static void Init();

private:
    static void *AllocPointerNode();
    static void FreePointerNode(IMPointerNode *pointer);

    static THR_LOCAL IMPointerList m_tab[ImcsMaxPointersArryLen];
    static THR_LOCAL uint64 m_count;

    static THR_LOCAL uint32 m_ptrNodeCacheCount;
    static THR_LOCAL IMPointerNode *m_ptrNodeCache[ImcsMaxPtrNodeCacheLen];
};

#endif /* IMCS_MEM_ALLOC_H */
